<?php
class MySnmp{
	private $serverTaleName;
	private $SqlLink;
	private $post = array();
	
	public function __construct($post,$serverTaleName,$sqlLike){
		$this->serverTaleName = $serverTaleName;
		$this->SqlLink = $sqlLike;
		$this->post = $post;
	}
	
	public function show(){
		
		/*
		*1.代码运行服务器 snmp安装	(可能不需要安装)
		*2.监控服务器snmp安装
		*3.监控服务器开启	161端口
		*4.监控服务器snmpd.conf 配置文件需配置（配置完需重启）
		
		安装snmp服务
		CentOS/RedHat下可以只用yum命令进行安装。
		需要安装的大概有一下几个：
		yum install -y net-snmp

		yum install -y net-snmp-devel

		yum install -y net-snmp-libs

		yum install -y net-snmp-perl

		yum install -y mrtg

		其中有一些不是必须的， -y 指的是对所有问题回答yes，省去安装时的交互。

		若要使用snmpwalk进行安装检测，则还需要安装net-snmp-utils包
		yum –y install net-snmp-utils
		Ubuntu可以通过apt-get install snmp snmpd 进行安装
		
		重启snmp服务
		service  snmpd  restart
		
		启动snmp服务
		service snmpd start
		用以下命令检查服务是否启动成功
		snmpwalk -v 2c -c sdomonitor 127.0.0.1 system
		*/
		
		
		
		$snmpSql = "select host,community,[object_id],[node_ID] from [".$this->serverTaleName."] where [node_ID]=".$this->post['id'];
		$snmpParameter = $this->SSgetAll($snmpSql,1);
		if(!$snmpParameter){
			echo "<br/>";
			echo "<input type='hidden' id='node_ID' value='".$this->post['id']."'/>";
			echo "host:<input type='text' id='snmpHost'/>";
			echo "community:<input type='password' id='snmpCommunity'/>";
			echo "object_id:<input type='text' id='snmpObject_id'/>";
			echo "<br><br>";
			echo "<input type='button' id='type' onclick='snmpPreview()' value='预览'/>";
			echo " ";
			echo "<input type='button' id='type' onclick='snmpSave()' value='保存并预览'/>";
			echo "<br>";
			echo "<br>";
			echo "<div id='divcss5'>";
			exit;
		}
		echo "<br/>";
		echo "<input type='hidden' id='node_ID' value='".$snmpParameter[0]['node_ID']."'/>";
		echo "host:<input type='text' id='snmpHost' value='".$snmpParameter[0]['host']."'/>";
		echo "community:<input type='password' id='snmpCommunity' value='".$snmpParameter[0]['community']."'/>";
		echo "object_id:<input type='text' id='snmpObject_id' value='".$snmpParameter[0]['object_id']."'/>";
		echo "<br><br>";
		echo "<input type='button' id='type' onclick='snmpPreview()' value='预览'/>";
		echo " ";
		echo "<input type='button' id='type' onclick='snmpSave()' value='保存并预览'/>";
		echo "<br>";
		echo "<br>";
		echo "<div id='divcss5'>";
		//检测参数是否为空and sql是否为设置
		if(ctype_space($snmpParameter[0]['host']) || !$snmpParameter[0]['host']){
			echo "snmpHost 未设置";
			exit;
		}else{
			if(ctype_space($snmpParameter[0]['community']) || !$snmpParameter[0]['community']){
				echo "snmpCommunity 未设置";
				exit;
			}else{
				if(ctype_space($snmpParameter[0]['object_id']) || !$snmpParameter[0]['object_id']){
					echo "snmpObject_id 未设置";
					exit;
				}
			}
		}
		
		$a = @snmpwalk($snmpParameter[0]['host'],$snmpParameter[0]['community'],$snmpParameter[0]['object_id']);
		if(!$a){
			echo "查询失败";
			exit;
		}
		foreach ($a as $val) {
			var_dump($val);
		}
		
		
/* echo "内存大小:"."<br>";
		$a = snmpwalk("192.168.1.228", "public", ".1.3.6.1.2.1.1");
		foreach ($a as $val) {
			var_dump($val);
		}die; */
		
	/* 	echo "内存大小:"."<br>";
		$a = snmpwalk("192.168.227.128", "public", ".1.3.6.1.2.1.25.2.2.0");
		foreach ($a as $val) {
			var_dump($val);
		}
		
		echo "CPU的当前负载，N个核就有N个负载:"."<br>";
		$a = snmpwalk("192.168.227.128", "public", ".1.3.6.1.2.1.25.3.3.1.2");
		foreach ($a as $val) {
			var_dump($val);
		} */
		echo "</div id='divcss4'>";
	} 
	//预览
	public function snmpPreview(){
		
		 /* $session = new SNMP(SNMP::VERSION_1, "192.168.227.128", "public");
		  $fulltree = $session->walk(".1.3.6.1.2.1.1");
		  var_dump($fulltree);
		  $session->close();
		die; */
		
		
		
		
		//var_dump($this->post);die;
		//检测参数是否为空and sql是否为设置
		if(ctype_space($this->post['snmpHost']) || !$this->post['snmpHost']){
			echo "snmpHost 未设置";
			exit;
		}else{
			if(ctype_space($this->post['snmpCommunity']) || !$this->post['snmpCommunity']){
				echo "snmpCommunity 未设置";
				exit;
			}else{
				if(ctype_space($this->post['snmpObject_id']) || !$this->post['snmpObject_id']){
					echo "snmpObject_id 未设置";
					exit;
				}
			}
		}
		//var_dump(md5($this->post['snmpCommunity']));
		//$z = snmpget($this->post['snmpHost'],$this->post['snmpCommunity'],"iso.1.3.6.1.4.1.2021.11.9.0");
		//var_dump($z);die;
	/*	echo "所有类";
		echo "<br/>";
		$a = @snmpwalk($this->post['snmpHost'],$this->post['snmpCommunity'],$this->post['snmpObject_id']);
		if(!$a){
			echo "查询失败";
			exit;
		}
		foreach ($a as $val) {
			var_dump($val);
		} */
		echo "<br/>";
		echo "网络本体树状信息";
		echo "<br/>";
		$b = snmpwalkoid($this->post['snmpHost'],$this->post['snmpCommunity'],$this->post['snmpObject_id']); 
		var_dump($b);

	
	
	}
	//保存并预览
	public function snmpSave(){
		//检测参数是否为空and sql是否为设置
		if(ctype_space($this->post['snmpHost']) || !$this->post['snmpHost']){
			echo "snmpHost 未设置";
			exit;
		}else{
			if(ctype_space($this->post['snmpCommunity']) || !$this->post['snmpCommunity']){
				echo "snmpCommunity 未设置";
				exit;
			}else{
				if(ctype_space($this->post['snmpObject_id']) || !$this->post['snmpObject_id']){
					echo "snmpObject_id 未设置";
					exit;
				}
			}
		}
		//检测sqlserver表是否存在关联节点信息
		$IS_node = $this->SSgetAll("select [node_ID] from [".$this->serverTaleName."] where [node_ID]=".$this->post['node_ID'],1);
		if(!$IS_node){
			//创建关联节点server连接
			$carSql = "insert into [".$this->serverTaleName."]([node_ID]) values(".$this->post['node_ID'].")";
			$this->SSquery($carSql,1);
		}
		
		
		$upSnmpSql = "update [".$this->serverTaleName."] set [host]='".$this->post['snmpHost']."',[community]='".$this->post['snmpCommunity']."',[object_id]='".$this->post['snmpObject_id']."' where [node_ID]=".$this->post['node_ID'];

		$this->SSgetAll($upSnmpSql,1);
		
		$a = @snmpwalk($this->post['snmpHost'],$this->post['snmpCommunity'],$this->post['snmpObject_id']);
		if(!$a){
			echo "查询失败";
			exit;
		}
		foreach ($a as $val) {
			var_dump($val);
		}
	}
	/**********************sqlServer*************************************************/
	public function SSquery($sql,$error){
        $result = sqlsrv_query($this->SqlLink,$sql);
		
		//检测sql执行是否成功
		if(!$result){
			echo json_encode($error);
			exit;
		}else{
			return true;
		}
    }

	//sql server输出查询结果
    public function SSgetAll($SSsql,$error){
        $result = sqlsrv_query($this->SqlLink,$SSsql);
		//检测sql执行是否成功
		if(!$result){
			echo json_encode($error);
			exit;
			//return $error;
		}
		unset($ary);
		$ary = array();
		$i = 0;
        while($row = sqlsrv_fetch_object($result))
        {
			$i++;
			unset($rowss);
			unset($values);
			$rowss = array();
			$values = array();
			//var_dump($row);
			$rows = $this->objectToArray($row);
			///* if(!empty($MStable['field'])){
				foreach($rows as $key=>$value){
					$rows[$key] = iconv("GB18030","utf-8",$value);
				}
			//} */
            $ary[] = $rows;
        }
        return $ary;
    }
	//递归将sqlserver查询结果对象转化为数绿
	public function objectToArray($row){
		$arr = is_object($row) ? get_object_vars($row) : $row;		
		if(is_array($arr)){
			foreach($arr as $key=>$value){
				if(is_object($value)){
					$data[$key] = $this->objectToArray($value);
				}else{
					$data[$key] = $value;
				}
				
			}
			return $data;
		}else{
			return $arr;
		}
		return $arr;
	}
}
?>